//
//  StoresViewController.m
//  BMProject
//
//  Created by chris on 16/1/1.
//  Copyright © 2016年 BMProject. All rights reserved.
//

#import "StoresViewController.h"
#import "StoresTopView.h"
#import "StoresCell.h"
#import "cardChooseView.h"
#import "DataModel.h"
#import "StoresDetailViewController.h"

@interface StoresViewController () <UITableViewDataSource, UITableViewDelegate, cardChooseViewDelegate, UITextFieldDelegate> {
    cardChooseView *cardChooseView1;
    SelectTypeBean *selectCardBean;
    StoresTopView *topView;
    UITableView *mytableView;
    NSMutableArray *_tableDataArr;//StoresListModel 类型
}
@end

@implementation StoresViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"待审核信息列表";
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 140)];
    bgView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];

    topView = [[[NSBundle mainBundle] loadNibNamed:@"StoresTopView" owner:self options:nil] firstObject];
    topView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, 130);
    topView.statusTF.placeholder = @"--请选择--";
    cardChooseView1 = [[[NSBundle mainBundle] loadNibNamed:@"cardChooseView" owner:nil options:nil] lastObject];
    cardChooseView1.delegate = self;
    NSMutableArray *cardList = [NSMutableArray array];
    [cardList addObjectsFromArray:@[[SelectTypeBean beanWithName:@"待审核"],
            [SelectTypeBean beanWithName:@"渠道经理审核通过"],
            [SelectTypeBean beanWithName:@"渠道经理审核驳回"],
            [SelectTypeBean beanWithName:@"分公司审核驳回"],
    ]];
    cardChooseView1.cardList = cardList;
    topView.statusTF.inputView = cardChooseView1;
    topView.statusTF.delegate = self;
    [topView.searchBtn addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:topView];

    UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.sectionFooterHeight = 10;
    tableview.sectionHeaderHeight = 10;
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableview.tableHeaderView = bgView;
    tableview.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        super.page = 1;
        [self requestAuditListWithPage:kIntToString(super.page)];
    }];
    tableview.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        super.page++;
        [self requestAuditListWithPage:kIntToString(super.page)];
    }];
    mytableView = tableview;
    [self addSubview:tableview];

    _tableDataArr = [NSMutableArray array];
    super.page = 1;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestAuditListWithPage:kIntToString(super.page)];
}


#pragma mark-UITableViewDateSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  _tableDataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"StoresCell";
    StoresCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"StoresCell" owner:self options:nil] lastObject];
    }
    [cell setVc:self];
    [cell setInfo:_tableDataArr[indexPath.row]];
    return cell;
}

#pragma mark- UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    StoresDetailViewController *detail = [[StoresDetailViewController alloc] init];
    detail.model = _tableDataArr[indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
}

#pragma mark-cardChooseViewDelegate
- (void)cancel {
    [self.view endEditing:YES];
}

- (void)chooseCard:(SelectTypeBean *)cardInfo {
    selectCardBean = cardInfo;
    topView.statusTF.text = cardInfo.name;
    [self.view endEditing:YES];
}

- (void)cardChooseView:(UIPickerView *)cardChooseView didSelectRow:(NSInteger)row withCardBean:(SelectTypeBean *)cardInfo {
    selectCardBean = cardInfo;
    topView.statusTF.text = cardInfo.name;
}

#pragma mark-event
- (void)searchAction:(UIButton *)searchButton {
    DLog(@"搜索事件--");
    [self requestAuditListWithPage:@"1"];
}

#pragma mark- 网络请求
- (void)requestAuditListWithPage:(NSString *)page {
    NSString *sign;
    if([topView.statusTF.text isEqualToString:@"待审核"]){
        sign = @"1";
    }else if([topView.statusTF.text isEqualToString:@"渠道经理审核通过"]) {
        sign = @"2";
    }else if([topView.statusTF.text isEqualToString:@"渠道经理审核驳回"]) {
        sign = @"3";
    }else if([topView.statusTF.text isEqualToString:@"分公司审核通过"]) {
        sign = @"4";
    }else if([topView.statusTF.text isEqualToString:@"分公司审核驳回"]) {
        sign = @"5";
    }else{
        sign = @"";
    }
    [UserServices auditByToken:[[AppDelegate appDelegate] getUserInfo].token
                      pageSize:kIntToString(kListPageSize)
                          page:kIntToString(super.page)
                          sign:sign
                          name:topView.nameTF.text
                      servicer:topView.numTF.text
               completionBlock:^(int result, id responseObject) {
                   DLog(@"order = %@",responseObject);
                   if (result == API_Request_Success) {

                       NSDictionary *dictionary1 = responseObject[0];

                       NSArray *WebsitePageDict = [dictionary1 objectForKey:@"WebsitePage"];
                       NSMutableArray *list = [NSMutableArray array];
                       [WebsitePageDict enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                           StoresListModel *model = [[StoresListModel alloc] initWithDictionary:obj error:nil];
                           [list addObject:model];
                       }];

                       NSArray *sysUserDict = [dictionary1 objectForKey:@"sysUser"];

                       if (super.page == 1) {
                           [_tableDataArr removeAllObjects];
                           [_tableDataArr addObjectsFromArray:list];
                           [mytableView.footer resetNoMoreData];//重置有更多数据
                           if (_tableDataArr == nil || _tableDataArr.count == 0) {
                               //提示无数据
                               [SVProgressHUD showErrorWithStatus:@"查无数据"];
                           } else {
                           }
                       }
                       else
                           [_tableDataArr addObjectsFromArray:list];
                       if (list.count == 0) {
                           super.page--;
                       }
                       [mytableView.header endRefreshing];
                       [mytableView.footer endRefreshing];
                       [mytableView reloadData];
                       if (list.count < kListPageSize)
                           [mytableView.footer noticeNoMoreData];//没有更多数据
                   } else {
                       [mytableView.header endRefreshing];
                       [mytableView.footer endRefreshing];
                   }
               }];
}

#pragma mark UITextFieldDelegate 限制TextField位数的代理方法 12为
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField == topView.statusTF) {
        NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if (toBeString.length > 0 && range.length!=1){ //最大0个字符
            textField.text = [toBeString substringToIndex:0];
            return NO;
        }
        if ([string chenkInputNSCharacterSetWithType:6])//kEmpty
        {
            return YES;
        }
        else
            return NO;
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)getSignNameFromid:(NSInteger)signId {
    // sign==1 待审核  sign==2 渠道经理审核通过  sign==3 渠道经理审核驳回  sign==4  分公司审核通过  sign==5 分公司审核驳回  sign==6 签约完成
    if (signId  == 1) {
        return @"待审核";
    }
    if (signId  == 2) {
        return @"渠道经理审核通过";
    }
    if (signId  == 3) {
        return @"渠道经理审核驳回";
    }
    if (signId  == 4) {
        return @"分公司审核通过";
    }
    if (signId  == 5) {
        return @"分公司审核驳回";
    }
    if (signId  == 6) {
        return @"签约完成";
    }
    return @"";
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
